package com.music.factory;

import com.music.constant.SmsProperties;
import cn.ucloud.common.pojo.Account;
import cn.ucloud.usms.client.DefaultUSMSClient;
import cn.ucloud.usms.client.USMSClient;
import cn.ucloud.usms.model.SendUSMSMessageParam;
import cn.ucloud.usms.model.SendUSMSMessageResult;
import cn.ucloud.usms.pojo.USMSConfig;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 发送手机短信
 */
@Component
public class PhoneSender implements Sender{

	private static USMSClient client = null;

	private String templateId = "UTA2204080MXOKP"; // 消息模板

	static {
		 client = new DefaultUSMSClient(new USMSConfig(
				new Account(SmsProperties.PRIVATE_KEY,SmsProperties.PUBLIC_KEY)
		));
	}

	/**
	 * 发送手机验证码
	 * @param phone
	 * @param code
	 */
	@Override
	public boolean send(String phone, String code) {

		List<String> phoneNumbers = new ArrayList<>();
//		phoneNumbers.add("19974812527");
		phoneNumbers.add(phone);
		System.out.println("发送的短信手机号：" + phone);

		SendUSMSMessageParam param = new SendUSMSMessageParam(phoneNumbers, templateId);

		param.setSigContent("SIG202204077E5654");
		param.setProjectId("org-bv3f0a");

		List<String> templateParams = new ArrayList<>();
		templateParams.add(code);
		param.setTemplateParams(templateParams);
		SendUSMSMessageResult result = null;
		try {
			result = client.sendUSMSMessage(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(result);

		return result != null && result.getRetCode().equals(0);
	}
}
